﻿package fwk.siteManager {	

	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.None;
	import fl.transitions.TweenEvent;
	import fwk.siteManager.BO.PageData;
	import fwk.siteManager.BO.RessourceData;
	import fwk.siteManager.BO.TemplateData;
	import fwk.siteManager.interfaces.ITemplate;
	
	import fwk.siteManager.events.TemplateEvent;
	import fwk.siteManager.events.ContainerLoaderEvent;
	import fwk.loaderManager.LoaderItemGroup;
	import fwk.loaderManager.LoaderItemObject;
	
//	import fwk.interfaces.IPage;
	
	public class Template extends MovieClip implements ITemplate
	{
		
		///////////////////////////
		// private properties
		private var _uiTemplate:String = "";
		private var _uipage:String 		= "";
		private var _pageData:PageData;
		private var _templateData:TemplateData;
		
		private var _initialized:Boolean = false;
		private var _deepLink:String;
		private var _ressources:Object; 					// contient les ressources du template
		private var _transition:Tween;
		
		private var _tabDisplayElements:Array;
		private var _nbElementClosed:Number = 0;
		
		public function get tabDisplayElements():Array
		{
			return _tabDisplayElements;
		}
		public function get xmlPage():XML
		{
			return _pageData.xmlDescription;
		}

		public function get xmlTemplate():XML
		{
			return _templateData.xmlDescription;
		}

		public function get deepLink():String
		{
			return _deepLink;
		}
		public function set deepLink(val:String):void
		{
			IMain.deepLink = val;
		}
		public function get uipage():String
		{
			return _uipage;
		}
		public function set uipage(val:String):void
		{
			_uipage = val;
		}
		public function get uiTemplate():String
		{
			return _uiTemplate;
		}
		public function set uiTemplate(val:String):void
		{
			_uiTemplate = val;
		}
		public function get ressources():Object
		{
			return _ressources;
		}
		public function get initialized():Boolean
		{
			return _initialized;
		}
		
		public function get pageData():PageData { return _pageData; }
		
		public function set pageData(value:PageData):void 
		{
			_pageData = value;
		}
		
		public function get templateData():TemplateData { return _templateData; }
		
		public function set templateData(value:TemplateData):void 
		{
			_templateData = value;
		}
		//-----------------------------------------------------------------------
		// Constructeur
		//-----------------------------------------------------------------------
		function Template ()
		{
			
			pageData = new PageData();
			templateData = new TemplateData();
			
		}
		
		//-----------------------------------------------------------------------
		// Methodes
		//-----------------------------------------------------------------------
		public final function _init():void
		{
			if (!_initialized) {
				
				trace("---------- init Page ----------");
				
				_tabDisplayElements = new Array();
				
				if(_templateData != null) _uiTemplate 	= _templateData.id;
				_uipage 		= _pageData.id;

				this.x 			= _pageData.x;
				this.y 			= _pageData.y;

				_deepLink = IMain.deepLink;
					
				
				// init des ressources
				_ressources = new Object();


				var resData:RessourceData;
				var element:Object;
				var k:int;

				var ressourcePage:LoaderItemGroup = IMain.getLoaderItemPage(_uipage) as LoaderItemGroup;
				if (_pageData != null && ressourcePage != null)
				{

					
					
					trace("ressourcePage " + ressourcePage);
					trace("ressourcePage.nbItem " + ressourcePage.nbItem);
					trace("_pageData.listRessources.length" + _pageData.listRessources.length);
					
					for (k = 0; k < _pageData.listRessources.length;k++){
					
						resData = _pageData.listRessources[k] as RessourceData;
						
						if (ressourcePage.tabItem[resData.id] != null) {
							
							
							trace("resData " + resData);
							trace("resData.id " + resData.id);
							trace("(ressourcePage.tabItem[resData.id] as LoaderItemObject).completed " + (ressourcePage.tabItem[resData.id] as LoaderItemObject).completed);
							
							element = (ressourcePage.tabItem[resData.id] as LoaderItemObject).content;
							_ressources[resData.id] = element;
							
							trace("element " + element);
							
							
							if (element is DisplayObject)
							{
								element.x = resData.x;
								element.y = resData.y;
									
								
								if (resData.display)
								{
									this.addChild(element as DisplayObject);
									if (resData.center) {
										element.x = IMain.width/2 - element.width/2;
										element.y = IMain.height/2 - element.height/2;
									}
								}
	
							}
							if (element is DisplayElement && IMain.xmlContentPage == null)
							{
								
								element._init(resData.id, resData.xmlDescription);
								element.addEventListener(TemplateEvent.CLOSED, onCloseElementFinish);
								element.template = this;
								_tabDisplayElements.push(element);

								
							}
						}

					
					}
				}
				
				
				if (_templateData != null) {
					
					var ressourceTemplate:LoaderItemGroup = IMain.getLoaderItemTemplate(_uiTemplate) as LoaderItemGroup;
					
					for (k = 0; k < _templateData.listRessources.length;k++){
					
						resData = _templateData.listRessources[k];

						if (ressourceTemplate.tabItem[resData.id] != null) {
						
							element = (ressourceTemplate.tabItem[resData.id] as LoaderItemObject).content;

							_ressources[resData.id] = element;
							
							if (element is DisplayObject)
							{
								element.x = resData.x;
								element.y = resData.y;
									
								if(resData.display){
									this.addChild(element as DisplayObject);
									if (resData.center) {
										element.x = IMain.width/2 - element.width/2;
										element.y = IMain.height/2 - element.height/2;
									}
								}
							}
							if (element is DisplayElement && IMain.xmlContentPage == null)
							{
								
								element._init(resData.id, resData.xmlDescription);
								element.addEventListener(TemplateEvent.CLOSED, onCloseElementFinish);
								element.template = this;
								_tabDisplayElements.push(element);

								
							}
						}
					
					}
				}
				
				_initialized = true;
				
				dispatchEvent(new TemplateEvent(TemplateEvent.ON_READY));
				
			}
			
			// init elements
			for (var i:int = 0; i < tabDisplayElements.length; i++)
			{
				_tabDisplayElements[i].init();
			}
			init();
		}
		public final function onContextChange(evt:ContainerLoaderEvent):void
		{
			
			var tab:Array = evt.deepLink.split(IMain.splitDeepLink);

			
			if ( tab[1] == uipage) {
				IMain.debugIt("---------- onContextChange template ---------- " + evt.deepLink);
				

				_deepLink = evt.deepLink;
				onDeepLinkChange();
			}
			

			
		}
		public function init():void
		{
			
			
		}
		public final function _display():void
		{
	
			// ajout ecouteur context change
			IMain.addEventListener(ContainerLoaderEvent.CONTEXT_CHANGE, onContextChange);
			
			if (_tabDisplayElements.length > 0)
			{
				_nbElementClosed = 0;
				for (var i:int = 0; i < tabDisplayElements.length; i++)
				{
					_tabDisplayElements[i].display();
				}
			}
			
			display();
			
		}
		public function display():void
		{
			IMain.debugIt("---------- display Page ----------");

			
			if (_tabDisplayElements.length == 0){
				this.alpha = 0;
				if(_transition != null) _transition.stop();
				_transition = new Tween(this, "alpha" , None.easeNone, this.alpha, 1, 20, false);
				_transition.addEventListener(TweenEvent.MOTION_FINISH, onDisplayTweenFinish);
			}
			
		}
		public function close():void
		{
			IMain.debugIt("---------- close Page ----------");
			
			if (_tabDisplayElements.length > 0)
			{
				for (var i:int = 0; i < tabDisplayElements.length; i++)
				{
					tabDisplayElements[i].close();
				}
			}else
			{
				if(_transition != null) _transition.stop();
				_transition = new Tween(this, "alpha" , None.easeNone, this.alpha, 0, 20, false);
				_transition.addEventListener(TweenEvent.MOTION_FINISH, onCloseTweenFinish);
			}	

		}
		public function onCloseElementFinish(evt:TemplateEvent):void
		{
			_nbElementClosed++;
			
			IMain.debugIt("onCloseElementFinish " + _nbElementClosed);
			
			if (_tabDisplayElements.length == _nbElementClosed)
			{
				onCloseFinish();
			}
			
		}
		private function onCloseTweenFinish(evt:TweenEvent):void
		{
			_transition.removeEventListener(TweenEvent.MOTION_FINISH, onCloseFinish);
			onCloseFinish();
		}
		private final function onDisplayTweenFinish(evt:TweenEvent):void
		{
			_transition.removeEventListener(TweenEvent.MOTION_FINISH, onCloseFinish);
			onDisplayFinish();
		}
		public final function onDisplayFinish():void
		{
			IMain.debugIt("---------- onDisplayFinish Page ----------");

			dispatchEvent(new TemplateEvent(TemplateEvent.FINISHED));
		}
		public function onCloseFinish():void
		{
			IMain.debugIt("---------- onCloseFinish Page ----------");
			
			// retire ecouteur context change
			IMain.removeEventListener(ContainerLoaderEvent.CONTEXT_CHANGE, onContextChange);
			
			_initialized = false;
			
			this.alpha = 1;
			dispatchEvent(new TemplateEvent(TemplateEvent.CLOSED));
		}		
		public function dispose():void
		{
			// on se débarrase des référence des ressources
			_ressources = null;
			
			
			
		}
		public function onDeepLinkChange():void
		{
			for (var i:int = 0; i < tabDisplayElements.length; i++)
			{
				tabDisplayElements[i].onDeepLinkChange();
			}
		}
	}
}